package com.basic.business.linkBacklog.controller;


import com.basic.business.linkBacklog.domain.TLinkBacklog;
import com.basic.business.linkBacklog.service.ITLinkBacklogService;
import com.basic.business.pic.service.ITGoodsPicService;
import com.basic.common.annotation.FileExport;
import com.basic.common.annotation.Log;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.core.page.TableDataInfo;
import com.basic.common.enums.BusinessType;
import com.basic.common.utils.SecurityUtils;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.List;

import static com.basic.common.utils.DateUtils.getNowDate;

/**
 * 链接待办事项Controller
 *
 * @author zhaoxy_jn@163.com
 */
@RestController
@RequestMapping("/business/linkBacklog")
public class TLinkBacklogController extends BaseController
{
    @Resource
    private ITLinkBacklogService tLinkBacklogService;

    @Resource
    private ITGoodsPicService tGoodsPicService;

    /**
     * 查询链接待办事项列表
     */
    @PreAuthorize("@ss.hasPermi('business:linkBacklog:list')")
    @GetMapping("/list")
    public TableDataInfo list(TLinkBacklog tLinkBacklog)
    {
        startPage();
        List<TLinkBacklog> list = tLinkBacklogService.getList(tLinkBacklog);

        for (TLinkBacklog log : list){
            log.setImgUrl(tGoodsPicService.getPicUrl(log.getGoodsId()));
        }
        return getDataTable(list);
    }

    /**
     * 导出链接待办事项列表
     */
    @PreAuthorize("@ss.hasPermi('business:linkBacklog:export')")
    @Log(title = "链接待办事项", businessType = BusinessType.EXPORT)
    @FileExport(fileName = "链接待办事项.xlsx")
    @PostMapping("/export")
    public String export(TLinkBacklog tLinkBacklog) throws IOException
    {
        List<TLinkBacklog> list = tLinkBacklogService.getList(tLinkBacklog);
        return EasyExcelUtils.excelExport(TLinkBacklog.class,"链接待办事项数据", list);
    }

    /**
     * 获取链接待办事项详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:linkBacklog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tLinkBacklogService.get(id));
    }

    /**
     * 新增链接待办事项
     */
    @PreAuthorize("@ss.hasPermi('business:linkBacklog:add')")
    @Log(title = "链接待办事项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TLinkBacklog tLinkBacklog)
    {
        tLinkBacklog.setUserId(SecurityUtils.getUserId());
        tLinkBacklog.setDeptId(SecurityUtils.getDeptId());
        return toAjax(tLinkBacklogService.insertBacklog(tLinkBacklog));
    }

    /**
     * 修改链接待办事项
     */
    @PreAuthorize("@ss.hasPermi('business:linkBacklog:edit')")
    @Log(title = "链接待办事项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TLinkBacklog tLinkBacklog)
    {
        return toAjax(tLinkBacklogService.update(tLinkBacklog));
    }

    /**
     * 修改链接待办事项优化动作
     */
    @PostMapping("/editOptimizeActions")
    public AjaxResult editOptimizeActions(@RequestBody TLinkBacklog tLinkBacklog)
    {
        //优化时间
        tLinkBacklog.setOptimizeTime(getNowDate());

        Instant start = tLinkBacklog.getCreateTime().toInstant();
        Instant end = tLinkBacklog.getOptimizeTime().toInstant();

        // 计算时间差
        Duration duration = Duration.between(start, end);

        double hour = duration.toMinutes() / 60.0;

        String formatted = String.format("%.2f", hour);

        tLinkBacklog.setOptimizationDuration(Double.parseDouble(formatted));

        //已优化
        tLinkBacklog.setOptimizeStatus(1);

        return toAjax(tLinkBacklogService.update(tLinkBacklog));
    }

    /**
     * 删除链接待办事项
     */
    @PreAuthorize("@ss.hasPermi('business:linkBacklog:remove')")
    @Log(title = "链接待办事项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tLinkBacklogService.deleteByIds(ids));
    }

    /**
     * 定时处理超时24小时待办记录
     */
    @Scheduled(cron = "0 0 * * * *")
    public void handleTimeoutBacklog()
    {
        tLinkBacklogService.handleTimeoutBacklog();
    }
}
